<?php

namespace App\Admin\Controllers;

use App\Models\ExhibitorInformation;
use App\Models\ExhibitorMessage;
use App\Models\SafetyAgreement;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Hash;

class SafetyAgreementController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '安全责任书';
    protected $gridFilter = false; // 添加这一行来禁用筛选功能
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SafetyAgreement());
        $grid->filter(function ($filter) {
            // 如果你想要在禁用筛选的情况下保留搜索框，可以添加如下代码
             $filter->disableIdFilter();
            $filter->like('exhibitor_information.name',  trans('admin.keywords'));
        });

        $grid->disableCreateButton();//隐藏新增
        $grid->column('exhibitor_information.name', __('exhibitor_information.name'));

        $grid->column('created_at', __('safety_agreement.created_at'));
        $grid->column('files', __('safety_agreement.files'))->display(function ($files) {

            return "<a href='$files' target='_blank'>点击下载</a>";
        });
        $grid->actions(function ($actions) {

            //显示按钮、查看按钮
            $actions->disableView();
        });
        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
        });
        $grid->disableActions();


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SafetyAgreement::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('exhibitor_information.name', __('safety_agreement.operator_id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('files', __('Files'));
        $show->field('file_name', __('File name'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SafetyAgreement());
        $exhibitor_information = ExhibitorInformation::query()->where('status', 1)->pluck('name', 'id')->toArray();
//        $form->select('operator_id', __('safety_agreement.operator_id'))
//            ->options($exhibitor_information)->rules(['required']);


        $form->file('files', trans('safety_agreement.files'))->options(['maxFileSize' => 1024 * 5,
            'language' => 'zh',
            'msgSizeTooLarge' => '文件"{name}" (<b>{size}KB</b>) 超过了允许的最大上传大小<b>5 MB</b>.',
        ])->removable()->help('且不能超过5MB');
        $form->saving(function (Form $form) {
            $files = $form->files;
            $filename = $files->getClientOriginalName();

            $certificates_exhibitor_information_id = $form->operator_id;
            $form->model()->operator_name = ExhibitorInformation::query()->find($certificates_exhibitor_information_id)->name;
            $form->model()->file_name = $filename;


        });
        $form->tools(function ($tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();

        });
        $form->footer(function ($footer) {

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`查看`checkbox

        });


        return $form;
    }
}
